module.exports = app => class BindApi {

    async getToken(key) {
        try {
            return await this.get(`/token/${key}`);
        } catch (e) {
            if (e.status !== 404) {
                throw e;
            }
            return null;
        }
    }

    async setToken(key, token) {
        return await this.put(`/token/${key}`, { token });
    }

    async getWeixinUserInfo(openid) {
        return await this.get(`/wx-user/${app.config.weixin.appId}/${openid}`);
    }

    async initUnionId() {
        return await this.put(`wx/${app.config.weixin.appId}/initUnionId`);
    }

    async setWeixinUserInfo(openid, userInfo) {
        return await this.put(`/wx-user/${app.config.weixin.appId}/${openid}`, userInfo);
    }

    async getBindPatientByWeixin(openid) {
        try {
            const r = await this.get(`/bind/wx-user/${app.config.weixin.appId}/${openid}`);
            if (r && r.length) {
                return r[0];
            }
        } catch (e) {
            if (e.status !== 404) {
                throw e;
            }
        }
        return null;
    }

    async getBindWeixinByPatient(patientId) {
        try {
            const r = await this.get(`/bind/patient/${patientId}`);
            if (r && r.length) {
                return r[0];
            }
        } catch (e) {
            if (e.status !== 404) {
                throw e;
            }
        }
        return null;
    }

    async bindPatient(patientId, openId) {
        return await this.put(`/bind/patient/${app.config.weixin.appId}/${patientId}`, {
            openid: openId,
            single: 1
        });
    }

    async delBindPatient(openId) {
        return await this.delete(`/bind/wx-user/${openId}`);
    }

    async callWeixinApi(api, ...args) {
        return await this.post(`/wx/api/${app.config.weixin.appId}/${api}`, { args });
    }

    async sendMessageToMA(appId, openId, message) {
        return await this.post(`/sendMessageToMA/${appId}/${openId}`, message);
    }

    async sendBindMessageToMA(appid, openId, ticket) {
        return await this.post(`/sendBindMessageToMA/${appid}/${openId}`, { ticket });
    }

    async getWeixinInfo(appId) {
        return await this.get(`/wx/api/${appId}`);
    }


    async getQrCode(param) {
        return await this.get(`/qrCode`, { sceneId: param.sceneId, label: param.label, userType: 1, expire_seconds: 2592000 })
    }

    async insuranceConfigInfo(insuranceId) {
        return await this.get(`/insurances/${insuranceId}`);
    }

    async insuranceConfigList(query) {
        return await this.get(`/insurances`, query);
    }

    async getAds(query) {
        return await this.get(`/ads`, query);
    }

    async getInsuranceList(query) {
        return await this.get(`/insurances`, query);
    }

    async pointExchange() {
        return await this.get(`/pointExchange`);
    }

    async pointExchangeInfo(exchangeId) {
        return await this.get(`/pointExchange/${exchangeId}`);
    }

    async getStaticPage(pageId) {
        return await this.get(`/staticPage/${pageId}`);
    }
    async getInsuranceDiseaseInquiries(insurancePackageId) {
        return await this.get(`/insurance/disease/${insurancePackageId}`);
    }
    async createYsgcuser(query) {
        return await this.post(`/activeYsgc`, query);
    }
    async getYsgcuser(query) {
        return await this.post(`/getYsgc`, query);
    }
    async getenquiry(id) {
        //  node接口
        return await this.get(`/getenquiry/${id}`);
    }
}
